/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gettree;

import java.io.BufferedReader;

/**
 * Contruirea unui arbore din forma prefixata a acestuia 
 * @author user
 */
public class GetTree {
    
    private static TreeNode currentTree;
    
    public static Tree getTree (String string){
        throw  new UnsupportedOperationException();
        //Tree tree = new Tree();
        //return tree;
    }
    
    public static Tree getTree (String string,String[] wraper){
        throw  new UnsupportedOperationException();
        //Tree tree = new Tree();
        //return tree;
    }
    
    /**
     * 
     * @param string reprezentarea arborelui in forma prefixata
     * @param wraperStart delimatator pentru incepere enumerare fii
     * @param wraperEnd delimitator pentrun terminat enumerare fii
     * @param escape excape string
     * @param delim delimiatator enumerare fii
     * @return  arborele contruit 
     * @throws Exception 
     */
    public static Tree getTree (String string,String wraperStart , String wraperEnd , String escape , String delim) throws Exception{
    	Tree tree = new Tree();
        currentTree = tree.getRoot();
        //bufferul de nume
        StringBuffer lable =  new StringBuffer(); 
        boolean escaped = false;
        for (int i = 0 ; i < string.length() ; i++){
        	escaped = false;
        	// verifica daca ce grupul de caractere ce urmeaza a fost fost precedat de excape
        	if (i>=escape.length())
        		if (string.substring(i-escape.length()).startsWith(escape) )
        			escaped=true;
        	// verifica daca incepe/se termina lista de fii a nodului curent
            if ((!string.substring(i).startsWith(wraperStart) && !string.substring(i).startsWith(wraperEnd)) || escaped){
                // verificare daca am citit numele unui nod
            	if (string.substring(i).startsWith(delim)){
                    if (!lable.toString().trim().equals("")){
                    	// creeare nod si adaugarea in lista parintelui
	                    TreeNode node = new TreeNode(currentTree, lable.toString(), null);
	                    //golire buffer
	                    lable.delete(0, lable.length());
                    }
                }
                else {
                	// umplerea bufferului cu numele unui nod
                    lable.append(string.charAt(i));
                }
            }
            else {
            	//verifica daca s-a terminat enumerarea fiilor nodului
                if (string.substring(i).startsWith(wraperEnd)&& !escaped){
                    if (!lable.toString().trim().equals("")){
                    	// adauga ultimul nod
                        TreeNode node = new TreeNode(currentTree, lable.toString(), null);
                        // mergi inapoi cu un nicel mai sus in arbore
                        currentTree =(TreeNode)currentTree.getParent();
                        lable.delete(0, lable.length());
                    }
                    else{
                    	// mergi inapoi cu un nicel mai sus in arbore
                    	if (currentTree!=null)
                    		currentTree =(TreeNode)currentTree.getParent();
                    }
                }
                else 
                	// daca se incepe o lista de enumerare
                    if (!lable.toString().trim().equals("")){
                    	//creaza un nod nod in arbore ce va deveni radacina curenta 
	                    TreeNode node = new TreeNode(currentTree, lable.toString(), null);
	                    currentTree = node;
	                    lable.delete(0, lable.length());
                    }
            }
            
        }

        return tree;
    }
    
    public static Tree getTree (String string,String wraperStart , String wraperEnd , String escape , String delim,TreeNode cuTreeNode){
        Tree tree = new Tree();
        cuTreeNode = tree.getRoot();
        StringBuffer lable =  new StringBuffer(); 
        for (int i = 0 ; i < string.length() ; i++){
            if (!string.substring(i).startsWith(wraperStart) && !string.substring(i).startsWith(wraperEnd) ){
                if (string.substring(i).startsWith(delim)){
                    if (!lable.toString().trim().equals("")){
                    TreeNode node = new TreeNode(cuTreeNode, lable.toString(), null);
                    //currentTree.addChild(node);
                    lable.delete(0, lable.length());
                    }
                }
                else {
                    lable.append(string.charAt(i));
                }
            }
            else {
                if (string.substring(i).startsWith(wraperEnd)&& !string.substring(i-escape.length()).startsWith(escape)){
                    if (!lable.toString().trim().equals("")){
                        TreeNode node = new TreeNode(cuTreeNode, lable.toString(), null);
                        //currentTree.addChild(node);
                        cuTreeNode =(TreeNode)cuTreeNode.getParent();
                        lable.delete(0, lable.length());
                    }
                }
                else 
                    if (!lable.toString().trim().equals("")){
                    TreeNode node = new TreeNode(cuTreeNode, lable.toString(), null);
                    //currentTree.addChild(node);
                    cuTreeNode = node;
                    lable.delete(0, lable.length());
                    }
            }
            
        }
        return tree;
    }
    
    public static Tree getTreeFromFile (String string){
        throw  new UnsupportedOperationException();
        //Tree tree = new Tree();
        //return tree;
    }
    
    public static Tree getTree (BufferedReader buffer){
        throw  new UnsupportedOperationException();
        //Tree tree = new Tree();
        //return tree;
    }    
    public static void main (String [] g) throws Exception{
    	gettree.Tree t = getTree("function_`(_`)_(id(collatz),id(n),print`(_`)(_._(string(\"Testshfjds\"),_._(id(n),string(\"...\")))))", "(", ")", "`", ",");
    }
}
